$(function(){
	//localStorage.clear();  
	/*localStorage.removeItem('tableList');*/
    //获取本地存储数据，并且转换成对象
	function getdata(){
		var arr1 = [];
		var url =  "sys/userInfo/save" ;
        $.ajax({	
            type: "POST",
            url: baseURL + url,
            contentType: "application/json",
            data: JSON.stringify(vm.user),
            success: function(r){
               arr1 = r.olist;
            }
        });
        return arr1;
	}
    var arr = [];
    function getData(){
      if(localStorage.tableList == undefined){
        arr = getdata();
      }else{
        arr = JSON.parse(localStorage.tableList);
      }
      return arr;
    }
   // add();
    //把数据存到本地存储，并且转换成字符串格式的JSON
    function saveData(data){
    //  var data = getData();
    //  JSON.stringify(localStorage.tableList);
      localStorage.tableList = JSON.stringify(data);
    }
    //增加行方法
    function add(){
      $("#table1 tr:not(#table1 tr:first,#table1 tr:last)").remove();//每次增加行前删除前面的行，否则会重复增加
      var data = getData();
      $.each(data,function(i, v){
    	  $("<tr>").attr("index",i).html(
          		"<td contenteditable='true' data-role='datetime'>"+ v.datetime + "</td>"
          		+ "<td contenteditable='true' data-role='school'>" + v.school + "</td>"
          		+ "<td contenteditable='true' data-role='major'>" + v.major + "</td>"		 
          		+ "<td contenteditable='true' data-role='edu'>" + v.edu + "</td>"
          		+ "<td contenteditable='true' data-role='academic'>" + v.academic + "</td>" 
          		+ "<td contenteditable='true' data-role='nature'>" + v.nature + "</td>" 
          		+ "<td><button class='btn btn-danger btn-sm xldel'>删除</button></td>").insertBefore("#table1 tr:last");
      })
      saveData(data);
    }
    //点击增加按钮事件    
    $('#table1').on('click','.xladd',function(){
        var data = getData();
        data.push({"datetime": "", "school": "", "major": "","edu": "","academic": "","nature": ""});
        saveData(data);
        add();
       window.location.hash = "#table1";
    });
    //删除行方法，事件委派，根据当前点击的按钮的行的索引值
    $('#table1').on('click','.xldel',function(){
      var data = getData();
      var index = $(this).parent().parent().attr("index");
      data.splice(index,1);
      saveData(data);
      add();
    });
    //可编辑效果 contenteditable='true'
    $('#table1').on('blur','[contenteditable="true"]',function(){
      var data = getData();
      var index = $(this).parent().attr('index');
      var val = $(this).html();
      var attr = $(this).attr('data-role');
      data[index][attr] = val;
      saveData(data);
    });


    var arr2 = [];
    function getData2(){
      if(localStorage.tableList2 == undefined){
        arr2 = [];
      }else{
        arr2 = JSON.parse(localStorage.tableList2);
      }
      return arr2;
    }
	/*localStorage.removeItem('tableList');*/
    //获取本地存储数据，并且转换成对象
   
    //add2();
    //把数据存到本地存储，并且转换成字符串格式的JSON
    function saveData2(data){
    //  var data = getData();
    //  JSON.stringify(localStorage.tableList);
      localStorage.tableList2 = JSON.stringify(data);
    }
    //增加行方法
    function add2(){
      $("#table2 tr:not(#table2 tr:first,#table2 tr:last)").remove();//每次增加行前删除前面的行，否则会重复增加
      var data = getData2();
      $.each(data,function(i, v){
    	  $("<tr>").attr("index",i).html(
          		"<td contenteditable='true' data-role='datetime'>"+ v.datetime + "</td>"
          		+ "<td contenteditable='true' data-role='company'>" + v.company + "</td>"
          		+ "<td contenteditable='true' data-role='post'>" + v.post + "</td>"
          		+ "<td contenteditable='true' data-role='voucher'>" + v.voucher + "</td>" 
          		+ "<td contenteditable='true' data-role='telephone'>" + v.telephone + "</td>" 
          		+ "<td><button class='btn btn-danger btn-sm gzdel'>删除</button></td>").insertBefore("#table2 tr:last");
      })
      saveData2(data);
    }
    //点击增加按钮事件    
    $('#table2').on('click','.gzadd',function(){
        var data = getData2();
        data.push({"datetime": "", "company": "", "post": "", "voucher": "","telephone": ""});
        saveData2(data);
        add2();
       window.location.hash = "#table2";
    });
    //删除行方法，事件委派，根据当前点击的按钮的行的索引值
    $('#table2').on('click','.gzdel',function(){
      var data = getData2();
      var index = $(this).parent().parent().attr("index");
      data.splice(index,1);
      saveData2(data);
      add2();
    });
  //可编辑效果 contenteditable='true'
    $('#table2').on('blur','[contenteditable="true"]',function(){
      var data = getData2();
      var index = $(this).parent().attr('index');
      var val = $(this).html();
      var attr = $(this).attr('data-role');
      data[index][attr] = val;
      saveData2(data);
    });
})

